<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/base}">
<head>
    <title>案例库 - 维修故障排查系统</title>
</head>
<body>
    <div layout:fragment="content">
        <div class="row">
            <div class="col-12">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h1>案例库</h1>
                    <a th:href="@{/cases/new}" class="btn btn-primary">
                        <i class="fas fa-plus me-1"></i>新建案例
                    </a>
                </div>
                
                <div class="card">
                    <div class="card-header">
                        <div class="row align-items-center">
                            <div class="col-md-8">
                                <div class="input-group">
                                    <input type="text" class="form-control" id="searchInput" 
                                           placeholder="搜索设备名称、故障现象或解决方案..."
                                           th:value="${keyword}">
                                    <button class="btn btn-outline-secondary" type="button" onclick="searchCases()">
                                        <i class="fas fa-search"></i>
                                    </button>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <select class="form-select" id="difficultyFilter" onchange="filterCases()">
                                    <option value="">所有难度</option>
                                    <option value="简单">简单</option>
                                    <option value="中等">中等</option>
                                    <option value="困难">困难</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead class="table-light">
                                    <tr>
                                        <th>案例编号</th>
                                        <th>设备名称</th>
                                        <th>故障现象</th>
                                        <th>难度等级</th>
                                        <th>标签</th>
                                        <th>创建时间</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr th:each="case : ${cases}">
                                        <td>
                                            <strong th:text="${case.caseNumber}"></strong>
                                        </td>
                                        <td th:text="${case.deviceName}"></td>
                                        <td th:text="${#strings.abbreviate(case.faultPhenomenon, 50)}"></td>
                                        <td>
                                            <span class="badge" 
                                                  th:classappend="${case.difficultyLevel == '困难'} ? 'bg-danger' : 
                                                                 ${case.difficultyLevel == '中等'} ? 'bg-warning' : 'bg-success'"
                                                  th:text="${case.difficultyLevel}"></span>
                                        </td>
                                        <td>
                                            <span th:if="${case.tags}" 
                                                  th:each="tag : ${#strings.arraySplit(case.tags, ',')}" 
                                                  class="badge bg-secondary me-1" th:text="${tag}"></span>
                                        </td>
                                        <td th:text="${#temporals.format(case.createdAt, 'yyyy-MM-dd')}"></td>
                                        <td>
                                            <a th:href="@{/cases/{id}(id=${case.id})}" class="btn btn-sm btn-info">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <button class="btn btn-sm btn-warning" onclick="editCase([[${case.id}]])">
                                                <i class="fas fa-edit"></i>
                                            </button>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                        
                        <div class="d-flex justify-content-between align-items-center mt-3">
                            <div>
                                <small class="text-muted">显示 1-10 条，共 <span th:text="${#lists.size(cases)}">0</span> 条记录</small>
                            </div>
                            <nav>
                                <ul class="pagination pagination-sm mb-0">
                                    <li class="page-item disabled">
                                        <a class="page-link" href="#" tabindex="-1">上一页</a>
                                    </li>
                                    <li class="page-item active"><a class="page-link" href="#">1</a></li>
                                    <li class="page-item"><a class="page-link" href="#">2</a></li>
                                    <li class="page-item"><a class="page-link" href="#">3</a></li>
                                    <li class="page-item">
                                        <a class="page-link" href="#">下一页</a>
                                    </li>
                                </ul>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div layout:fragment="scripts">
        <script>
            function searchCases() {
                const keyword = document.getElementById('searchInput').value;
                const url = keyword ? `/cases?keyword=${encodeURIComponent(keyword)}` : '/cases';
                window.location.href = url;
            }
            
            function filterCases() {
                const difficulty = document.getElementById('difficultyFilter').value;
                // 实现难度筛选逻辑
                console.log('Filter by difficulty:', difficulty);
            }
            
            function editCase(caseId) {
                // 实现编辑案例逻辑
                console.log('Edit case:', caseId);
            }
            
            // 回车键搜索
            document.getElementById('searchInput').addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    searchCases();
                }
            });
        </script>
    </div>
</body>
</html>
